package customer

import (
	
	"goskeleton/app/global/consts"
	"goskeleton/app/service/customer/curd"
	"goskeleton/app/utils/response"
	"github.com/gin-gonic/gin"
)

type Customer struct{

}

func (cust *Customer) List(context *gin.Context) {
	page := context.GetFloat64(consts.ValidatorPrefix + "page")
	limit := context.GetFloat64(consts.ValidatorPrefix + "limit")
	name := context.GetString(consts.ValidatorPrefix + "name")
	company := context.GetString(consts.ValidatorPrefix + "company")
	customerStatus := context.GetFloat64(consts.ValidatorPrefix + "customer_status")


	
	// customerStatus := context.GetFloat64(consts.ValidatorPrefix + "customer_status")
	orderBy := "id asc"
	limitStart := (page - 1) * limit

	totalCounts, showList := curd.CreateCustomerCurdFactory().ListData(int(limitStart), int(limit), orderBy, name, company, int(customerStatus))

	if totalCounts > 0 && showList != nil {
		response.Success(context, consts.CurdStatusOkMsg, gin.H{"count": totalCounts, "data": showList})
	} else {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	}
}

func (cust *Customer) Detail(context *gin.Context) {
	customerId := context.GetFloat64(consts.ValidatorPrefix + "id")
	detail := curd.CreateCustomerCurdFactory().ShowOneItem(int(customerId))

	if detail == nil {
	
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, detail)
	}
}

func (cust *Customer) Create(context *gin.Context) {
	if curd.CreateCustomerCurdFactory().InsertData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg, "")
	}
}

func (cust *Customer) Edit(context *gin.Context) {
	if curd.CreateCustomerCurdFactory().UpdateData(context) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}



func (cust *Customer) GetCustomerIdAndNameAllData(context *gin.Context) {
	showList := curd.CreateCustomerCurdFactory().GetCustomerIdAndNameAllData()
	if showList == nil {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, showList)
	}
}	

func (cust *Customer) TotalStatistics(context *gin.Context) {
	totalStatusStatistics := curd.CreateCustomerCurdFactory().GetTotalStatusStatistics()
	if totalStatusStatistics == nil {
		response.Fail(context, consts.CurdSelectFailCode, consts.CurdSelectFailMsg, "")
	} else {
		response.Success(context, consts.CurdStatusOkMsg, totalStatusStatistics)
	}
}

func (cust *Customer) Delete(context *gin.Context) {
	customerId := context.GetFloat64(consts.ValidatorPrefix + "id")
	if curd.CreateCustomerCurdFactory().DeleteData(int(customerId)) {
		response.Success(context, consts.CurdStatusOkMsg, "")
	} else {
		response.Fail(context, consts.CurdDeleteFailCode, consts.CurdDeleteFailMsg, "")
	}
}